﻿using LightingPlatform.TCP;
using LightingPlatform.Tools;
using LightingPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;

namespace LightingPlatform.View.Level2
{
    public partial class LightingClassView : UserControl, IPageProvider
    {
        private CondProdClass _condition;

        public LightingClassView()
        {
            InitializeComponent();

            _condition = (CondProdClass)BusinessContext.Singleton.QueryParameters.Pop();
            if (_condition.HasClass)
            {
                ResourceProvider rs = new ResourceProvider();
                var item = (from r in rs.ProdTypes
                            where r.ItemID == _condition.Class
                            select r).FirstOrDefault();

                if(item != null)
                {
                    Title.Text = item.ItemName;
                }
            }

            Loading();
        }

        private LightingClassModel ConvertForm(ProdClass lighting)
        {
            LightingClassModel vm = new LightingClassModel();
            if(lighting.HasProdSeries)
                vm.ProdSeries = lighting.ProdSeries;

            if (lighting.HasProdLabel)
                vm.ProdLabel = lighting.ProdLabel;

            if (lighting.HasAdpClass)
                vm.AdpClass = lighting.AdpClass;

            if (lighting.HasPicFacade)
                vm.PicFacade = lighting.PicFacade;

            return vm;
        }

        private async void Loading()
        {
            WebServiceProxy proxy = new WebServiceProxy();
            ProdClassArray array = await proxy.SearchProdClass(_condition.ToByteArray());
            var query = from r in array.ProdClassList
                        group r by r.ProdLabel into g
                        select new
                        {
                            Key = g.Key,
                            Value = g
                        };

            List<LableItem> lableItems = new List<LableItem>();
            int j = 0;
            foreach (var item in query)
            {
                LableItem li = new LableItem();
                li.Navigation = Navigation;
                if (j++ > 0)
                {
                    li.Margin = new Thickness(0, 20, 0, 0);
                }
                li.Title.Text = item.Key;

                List<LightingClassModel> items = new List<LightingClassModel>();
                foreach (ProdClass child in item.Value)
                {
                    items.Add(ConvertForm(child));
                }
                li.Pictures.ItemsSource = items;
                lableItems.Add(li);
            }

            Grid grid = new Grid();

            int i = 0;
            foreach (LableItem item in lableItems)
            {
                RowDefinition row = new RowDefinition();
                row.Height = GridLength.Auto;
                grid.RowDefinitions.Add(row);
                grid.Children.Add(item);
                Grid.SetRow(item, i++);
            }

            LayoutRoot.Children.Add(grid);
            Grid.SetColumn(grid, 1);
        }

        public PageLevel PageLevel
        {
            get { return PageLevel.Second; }
        }


        public Frame Navigation
        {
            get;
            set;
        }
    }
}
